<?php

namespace App\Admin\Controllers;

use DB;
use Validator;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use App\SuperAgent;
use App\PublicGemsLog;
use App\Agent;

class SuperAgentController extends Controller
{
	/**
	 * [auditIndex 超级代理审核列表]
	 * @return [type] [description]
	 */
	public function auditIndex(Request $request)
	{
		$keywords = $request->input('keywords');
		$agents = SuperAgent::where(['status' => 0]);
		if ($keywords) {
			$agents = $agents->where(function($query) use ($keywords){
				$query->where('realname', 'like', '%'.$keywords.'%')->orWhere('mobile', 'like', '%'.$keywords.'%')->orWhere('nickname', 'like', '%'.$keywords.'%');
			});
		}
		$agents = $agents->paginate(15);
		$agents->appends([
			'keywords' => $keywords
			]);

		return view('admin.superagent.audit', compact('agents', 'keywords'));
	}


	/**
	 * [audit description]
	 * @return [type] [description]
	 */
	public function audit(SuperAgent $agent)
	{
		return view('admin.superagent.doaudit', compact('agent'));
	}

	/**
	 * [storeAudit description]
	 * @return [type] [description]
	 */
	public function storeAudit(SuperAgent $agent, Request $request)
	{
    	$validator = Validator::make($request->all(), [
			'status' => 'required',
        ], [
        	'status.required' => '选择状态',
        	]);
        if ($validator->passes()) {
        	$agent->status = $request->input('status');
        	$agent->reason = $request->input('reason');
        	$agent->divide = $request->input('divide');
        	$agent->pass_at = date('Y-m-d H:i:s', time());
        	$agent->save();
	    	return ['flag' => true, 'msg' => '设置成功'];
        } else {
        	return $validator->errors();
        }	
	}

	/**
	 * [index description]
	 * @return [type] [description]
	 */
	public function index(Request $request)
	{
		$keywords = $request->input('request');
		$agents = SuperAgent::where(['status' => 1]);
		if ($keywords) {
			$agents = $agents->where(function($query) use ($keywords){
				$query->where('realname', 'like', '%'.$keywords.'%')->orWhere('mobile', 'like', '%'.$keywords.'%')->orWhere('nickname', 'like', '%'.$keywords.'%');
			});
		}
		$agents = $agents->paginate(15);
		$agents->appends([
			'keywords' => $keywords
			]);

		return view('admin.superagent.index', compact('agents', 'keywords'));
	}

	/**
	 * [assignCard description]
	 * @return [type] [description]
	 */
	public function assignCard(SuperAgent $agent)
	{	
		return view('admin.superagent.assign', compact('agent'));
	}

	/**
	 * [storeCard description]
	 * @return [type] [description]
	 */
	public function storeCard(SuperAgent $agent, Request $request)
	{
		$validator = Validator::make($request->all(), [
			'gems' => 'required|numeric|min:1'
        ], [
        	'gems.required' => '请输入房卡数量',
        	'gems.numeric' => '房卡数量只能为整数',
        	'gems.min' => '房卡数量不少于1'
        	]);
        if ($validator->passes()) {
        	$agent->gems += $request->input('gems');
        	$log = new PublicGemsLog;
        	$log->type = 10;
        	$log->accept_id = $agent->id;
        	$log->gems = $request->input('gems');
			DB::beginTransaction();
	    	try{
	    		$agent->save();
	    		$log->save();
	        	DB::commit();

	        	return ['flag' => true, 'msg' => '分配成功'];
	    	} catch (Exception $e) {
	    		DB::rollback();
	    		return ['flag' => true, 'msg' => '事务执行失败，数据回滚'];
	    	}
        } else {
        	return $validator->errors();
        }	
	}

	/**
	 * [assignLog description]
	 * @param  SuperAgent $agent [description]
	 * @return [type]            [description]
	 */
	public function assignLog(SuperAgent $agent)
	{
		$logs = PublicGemsLog::where(['type' => 10, 'accept_id' => $agent->id])->paginate(15);
		return view('admin.superagent.assignlog', compact('logs', 'agent'));
	}

	/**
	 * [juniorEarn 下级绩效]
	 * @return [type] [description]
	 */
	public function juniorEarn(SuperAgent $agent, Request $request)
	{
		$keywords = $request->input('keywords');
		$agents = Agent::where(['superagent_id' => $agent->id]);
		if ($keywords) {
			$agents = $agents->where(function($query) use ($keywords){
				$query->where('nickname', 'like', '%'.$keywords.'%')->orWhere('realname', 'like', '%'.$keywords.'%')->orWhere('mobile', 'like', '%'.$keywords.'%')->orWhere('address', 'like', '%'.$keywords.'%');
			});
		}
		$agents = $agents->paginate(15);
		$agents->appends([
			'keywords' => $keywords
			]);

		return view('admin.superagent.juniorEarn', compact('agents', 'agent', 'keywords'));
	}


}	